package com.dmm.vod.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmm.interfacle.ResultCodeEnum;
import com.dmm.result.Result;
import com.dmm.vo.vod.TeacherQueryVo;
import com.dmm.vod.entity.Teacher;
import com.dmm.vod.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author dmm
 * @since 2022-08-19
 */
@Api(tags="讲师管理")
@RestController
@RequestMapping("/vod/teacher")
//@CrossOrigin
public class TeacherController {

    @Autowired
    TeacherService teacherService;



    @ApiOperation("查询所有讲师")
    @GetMapping("/findALL")
    public Result findALL(){

        List<Teacher> list = teacherService.list();

        return Result.success(list);
    }


    @ApiOperation("逻辑删除")
    @DeleteMapping("/deleteId/{id}")
    public Result deleteId(@PathVariable Long id){

        boolean b = teacherService.removeById(id);
        if(b){
            return Result.success(null);
        }
        return Result.fail(null);
    }

    @ApiOperation("查询删除的讲师")
    @GetMapping("/select")
    public Result select(){

        List<Teacher> list = teacherService.Logical_deletion();
        return Result.success(list);
    }


    @ApiOperation("分页查询")
    @PostMapping("{page}/{limit}")
    public Result index(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "teacherVo", value = "查询对象", required = false)
            // required = false传入参数可以为空
            @RequestBody(required = false) TeacherQueryVo teacherQueryVo) {
        //创建page对象，传递当前页和每页记录数
        Page<Teacher> pageParam = new Page<>(page, limit);
        //获取条件值
        String name = teacherQueryVo.getName();//讲师名称
        Integer level = teacherQueryVo.getLevel();//讲师级别
        String joinDateBegin = teacherQueryVo.getJoinDateBegin();//开始时间
        String joinDateEnd = teacherQueryVo.getJoinDateEnd();//结束时间
        //封装条件
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(name)) {
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)) {
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(joinDateBegin)) {
            wrapper.ge("join_date",joinDateBegin);
        }
        if(!StringUtils.isEmpty(joinDateEnd)) {
            wrapper.le("join_date",joinDateEnd);
        }
        //调用方法得到分页查询结果
        IPage<Teacher> pageModel = teacherService.page(pageParam, wrapper);
        return Result.success(pageModel);
    }


    @ApiOperation("批量删除")
    @DeleteMapping("/Batch_delete")
    public Result Batch_delete(@RequestBody List<Long> list){

        System.out.println(list);
        boolean b = teacherService.removeByIds(list);

        return b? Result.success(null):Result.fail(null);
    }

    @ApiOperation(value = "修改")
    @PutMapping("update")
    public Result update(@RequestBody Teacher teacher){

        boolean b = teacherService.updateById(teacher);
        return b? Result.success(null):Result.fail(null);
    }

    @ApiOperation(value = "新增")
    @PostMapping("save")
    public Result save(@RequestBody(required = false) Teacher teacher){

        boolean b = teacherService.save(teacher);

        return b? Result.success(null):Result.fail(null);
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("getbyid/{id}")
    public Result getbyid(@PathVariable Long id ){
        Teacher byId = teacherService.getById(id);
        return Result.success(byId);
    }
}

